<?php

include ('../session.php');
include ('../db.php');
include ('../functions.php');
include ('config.php');

$output = '';

$output .= "
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
  <head>
    <title>Panic Points - List Members</title>
    <link href=\"../general.css\" rel=\"stylesheet\" type=\"text/css\">
  </head>
  <body>";

include ('navigation.php');

if (!$sort)
{
  $sort = 'member_name';
}

$c_sort = mysql_clean ($sort);

if ($dir == 1)
{
  $c_dir = 'desc';
}
else
{
  $c_dir = 'asc';
}

$query = "select member_id, member_name, race_name, class_name, member_level, sum(earned) as earned, sum(spent) as spent, (sum(earned) - sum(spent)) as current
          from (select members.member_id, member_name, race_name, class_name, member_level, members.member_name, ifnull(drops.item_value / attendeecount, 0) as earned, (ifnull(drops2.item_value, 0) + ifnull(drops3.item_value, 0)) as spent
                from " . DKP_MEMBERS . " as members
                inner join " . DKP_RACES . " as races
                on races.race_id = members.race_id
                inner join " . DKP_CLASSES . " as classes
                on classes.class_id = members.class_id
                left outer join " . DKP_ATTENDEES . " as attendees
                on attendees.member_id = members.member_id
                left outer join " . DKP_DROPS . " as drops
                on drops.raid_id = attendees.raid_id
                left outer join " . DKP_DROPS . " as drops2
                on drops2.member_id = members.member_id
                and drops2.drop_id = drops.drop_id
                left outer join test_dkp_drops as drops3
                on drops3.member_id = members.member_id
                and attendees.member_id is null
                left outer join (select count(member_id) as attendeecount, raid_id
                                 from " . DKP_ATTENDEES . "
                                 group by raid_id) as attendeecounts
                on attendeecounts.raid_id = attendees.raid_id) as temp
                group by member_id, member_name
                order by $c_sort $c_dir";

$result = mysql_query ($query);

echo mysql_error ();

$output .= "
    <span class=\"listtitle\">members</span>
    <table class=\"list\" cellspacing=\"0\" cellpadding=\"0\">
      <tr class=\"listheader\">
        <td><a href=\"listmembers.php?sort=member_name&dir=" . ($dir ? 0 : 1) . "\">name</a></td>
        <td><a href=\"listmembers.php?sort=member_level&dir=" . ($dir ? 0 : 1) . "\">level</a></td>
        <td><a href=\"listmembers.php?sort=race_name&dir=" . ($dir ? 0 : 1) . "\">race</a></td>
        <td><a href=\"listmembers.php?sort=class_name&dir=" . ($dir ? 0 : 1) . "\">class</a></td>
        <td style=\"text-align: right\"><a href=\"listmembers.php?sort=earned&dir=" . ($dir ? 0 : 1) . "\">earned</a></td>
        <td style=\"text-align: right\"><a href=\"listmembers.php?sort=spent&dir=" . ($dir ? 0 : 1) . "\">spent</a></td>
        <td style=\"text-align: right\"><a href=\"listmembers.php?sort=current&dir=" . ($dir ? 0 : 1) . "\">current</a></td>
      </tr>";

$i = 0;
while ($row = mysql_fetch_array ($result))
{
  $i++;

  $output .= "
      <tr class=\"" . ($i & 1 ? "listodd" : "listeven") . "\">
        <td><a href=\"viewmember.php?memberid=" . $row['member_id'] . "\">" . $row['member_name'] . "</a></td>
        <td>" . $row['member_level'] . "</td>
        <td>" . $row['race_name'] . "</td>
        <td>" . $row['class_name'] . "</td>
        <td style=\"text-align: right\">" . number_format ($row['earned'], 2) . "</td>
        <td style=\"text-align: right\">" . number_format ($row['spent'], 2) . "</td>
        <td style=\"text-align: right; font-weight: bold\">" . number_format ($row['current'], 2) . "</td>
      </tr>";
	}

$output .= "
    </table>";

$output .= "
    <a href=\"addmember.php\">add member</a>";


$output .= "
  </body>
</html>";

echo $output;
